package com.eds.config;

import org.apache.tomcat.util.threads.ThreadPoolExecutor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
 * Created with IntelliJ IDEA.
 *
 * @ClassName：ThreedPoolConfig
 * @Author: vvSmile
 * @Date: 2025/04/09
 * @Description: 线程池配置
 */
@Configuration
public class ThreadPoolConfig {
    @Bean
    public ThreadPoolTaskExecutor recommendedThreadPoolExecutor() {
        // 创建线程池
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(5);    // 核心线程数
        executor.setMaxPoolSize(20);    // 最大线程数
        executor.setQueueCapacity(100); // 队列容量
        executor.setKeepAliveSeconds(60);// 线程空闲时间
        executor.setThreadNamePrefix("recommendation-");// 线程前缀名
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());// 拒绝策略
        executor.setWaitForTasksToCompleteOnShutdown(true);// 关闭线程池时等待所有任务完成
        executor.setAwaitTerminationSeconds(60);// 等待时间
        executor.initialize();// 初始化
        return executor;
    }
}
